package com.dferreira.languagesteach.data_adapters;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.util.Log;

import com.dferreira.languagesteach.LanguagesTeachApplication;
import com.dferreira.languagesteach.constants.Constants;
import com.dferreira.languagesteach.data_models.Subject;
import com.dferreira.languagesteach.data_parsers.XMLSubjectParser;
import com.dferreira.languagesteach.utils.ApplicationState;
import com.dferreira.repository.Repository;
import com.dferreira.repository.data_models.PackageMetadata;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

public class WorkoutAdapter {
    private final String TAG = "WorkoutAdapter";
    private String lessonFolder;
    private String selectedSubject;
    private Subject subject;
    private String packageInstalledFolder;
    private String imagesFolder;
    private final Activity activity;
    private boolean dataSetBuilt;

    /**
     * @param activity reference to the which the dataSource belongs
     */
    public WorkoutAdapter(Activity activity, String packageInstalledFolder) {
        this.activity = activity;
        this.dataSetBuilt = false;
        this.packageInstalledFolder = packageInstalledFolder;
    }

    /**
     * @return true -> Everything alright
     * false -> Some error
     */
    public boolean buildDataSet() {
        Repository repository;
        PackageMetadata imagesPkg;
        ApplicationState applicationState;

        applicationState = LanguagesTeachApplication.getApplicationState();

        this.lessonFolder = applicationState.getLessonFolder();
        this.selectedSubject = applicationState.getSubject();

        this.subject = parseSubjectXML();

        if (this.subject == null) {
            this.dataSetBuilt = false;
            return false;
        }
        repository = new Repository(activity);
        imagesPkg = repository.getInstalledPackageInfo(subject.getImagesFolder());
        if (imagesPkg != null) {
            imagesFolder = imagesPkg.getInstalledPath();
            this.dataSetBuilt = true;
            return true;
        } else {
            this.dataSetBuilt = false;
            return false;
        }
    }

    /**
     * Parse the subject.xml file
     *
     * @return a model class subject
     */
    private Subject parseSubjectXML() {
        // Local variables
        SAXParserFactory spf;
        SAXParser sp;
        XMLReader xr;
        XMLSubjectParser xmlSubjectParser;
        InputStream inputStream;
        InputSource inputSource;
        File file;
        String subjectFileName;

        subjectFileName = Constants.SUBJECT_FILE_NAME;
        try {
            // parsing from XML file
            spf = SAXParserFactory.newInstance();
            sp = spf.newSAXParser();
            xr = sp.getXMLReader();
            xmlSubjectParser = new XMLSubjectParser();
            xr.setContentHandler(xmlSubjectParser);

            file = new File(packageInstalledFolder +
                    File.separator + lessonFolder + File.separator
                    + selectedSubject + File.separator + subjectFileName);
            inputStream = new FileInputStream(file);
            inputSource = new InputSource(inputStream);
            inputSource.setEncoding(Constants.XML_ENCODING);
            xr.parse(inputSource);
            inputStream.close();
            return xmlSubjectParser.getSubject();
        } catch (Exception e) {
            Log.e(TAG, TAG, e);
            return null;
        }
    }

    /**
     * @param index position index
     * @return a description from the entry at position index
     */
    public String getImageDescription(int index) {
        return subject.getEntriesLst().get(index).getImageDescription();
    }

    /**
     * @param index position index
     * @return a specific drawable from the entry at position index
     */
    public Drawable getImageView(int index) {
        String imagePath;

        imagePath = imagesFolder + File.separator
                + subject.getEntriesLst().get(index).getImagePath();
        return Drawable.createFromPath(imagePath);
    }

    /**
     * @param index position index
     * @return a specific file description from the file which describe the
     * entry at position index
     */
    public String getAudioDescriptor(int index) {
        String audioPath;

        audioPath = packageInstalledFolder
                + File.separator + lessonFolder
                + File.separator + selectedSubject
                + File.separator + subject.getEntriesLst().get(index).getSoundPath();
        return audioPath;

    }

    /**
     * @return number of entries available at that subject
     */
    public int getNumberOfEntries() {
        return subject.getEntriesLst().size();
    }

    /**
     * @return true    ->  Was not possible provide a valid data set
     * false   ->  Everything was correctly set
     */
    public boolean wasNotDataSetBuilt() {
        return !dataSetBuilt;
    }
}
